<table width="100%"><tr><td width="20%">
<a href="../index.html">&lt; index</a><br />
<a href="index.html">&lt; 9. Pseudorandom number generator</a><br />
<a href="random_init.html">&lt; 9.1 Creating a generator</a><br />
</td><td width="60%">
<p align="center">
=====================================<br />
9.2 Using a generator<br />
=====================================<br />
</p></td><td width="20%">
<a href="random_array.html">&gt; 9.3 Getting a random int from a byte array</a>
</td></tr></table>
<style type="text/css">
.code { color:#444444; background-color:#EEEEEE;}
pre { color:#444444; background-color:#EEEEEE;}
table.param td { border : 1px solid #000000; }
th { background-color: #44BBFF; color: #FFFFFF }
table.none td { border : 0 }
</style>
Once you obtained a generator (using one of <a href="random_init.html">those methods</a>), you can get random numbers using following functions :<br />
To get a random integer :<br />
<p class="code"><pre>
C++ : int TCODRandom::getInt(int min, int max)
C   : int TCOD_random_get_int(TCOD_random_t mersenne, int min, int max)
Py  : random_get_int(mersenne, mi, ma)
</pre></p>
<table class="param">
<tr><th>Parameter</th><th>Description</th></tr>
<tr><td>mersenne</td><td>In the C version, the generator handler, returned by the <a href="random_init.html">initialization functions. If NULL, the default generator is used.</a>.<br /></td></tr>
<tr><td>min, max</td><td>Range of values returned. Each time you call this function, you get a number between (including) min and max<br /></td></tr>
</table>
<br />
To get a random floating point number :<br />
<p class="code"><pre>
C++ : float TCODRandom::getFloat(float min, float max)
C   : float TCOD_random_get_float(TCOD_random_t mersenne, float min, float max)
Py  : random_get_float(mersenne, mi, ma)
</pre></p>
<table class="param">
<tr><th>Parameter</th><th>Description</th></tr>
<tr><td>mersenne</td><td>In the C version, the generator handler, returned by the <a href="random_init.html">initialization functions</a>. If NULL, the default generator is used.<br /></td></tr>
<tr><td>min, max</td><td>Range of values returned. Each time you call this function, you get a number between (including) min and max<br /></td></tr>
</table>
<br />
Example :<br />
<p class="code"><pre>
C++ : // default generator
      TCODRandom * default = TCODRandom::getInstance();
      int aRandomIntBetween0And1000 = default->getInt(0,1000);
      // another random generator
      TCODRandom *myRandom = new TCODRandom();
      float aRandomFloatBetween0And1000 = myRandom->getFloat(0.0f,1000.0f);
C   : /* default generator */
      int a_random_int_between_0_and_1000 = TCOD_random_get_float(NULL,0,1000);
      /* another random generator */
      TCOD_random_t my_random = TCOD_random_new();
      float a_random_float_between_0_and_1000 = TCOD_random_get_float(my_random,0.0f,1000.0f);
Py  : # default generator 
      a_random_int_between_0_and_1000 = libtcod.random_get_float(0,0,1000)
      # another random generator 
      my_random = libtcod.random_new()
      a_random_float_between_0_and_1000 = libtcod.random_get_float(my_random,0.0,1000.0)
</pre></p>
<br />
You can save the state of a generator with :<br />
<p class="code"><pre>
C++ : TCODRandom *TCODRandom::save() const
C   : TCOD_random_t TCOD_random_save(TCOD_random_t mersenne)
Py  : random_save(mersenne)
</pre></p>
<table class="param">
<tr><th>Parameter</th><th>Description</th></tr>
<tr><td>mersenne</td><td>In the C version, the generator handler, returned by the <a href="random_init.html">initialization functions</a>. If NULL, the default generator is used.<br /></td></tr>
</table>
<br />
And restore it later. This makes it possible to get the same serie of number several times with a single generator.<br />
<p class="code"><pre>
C++ : void TCODRandom::restore(const TCODRandom *backup)
C   : void TCOD_random_restore(TCOD_random_t mersenne, TCOD_random_t backup)
Py  : random_restore(mersenne, backup)
</pre></p>
<table class="param">
<tr><th>Parameter</th><th>Description</th></tr>
<tr><td>mersenne</td><td>In the C version, the generator handler, returned by the <a href="random_init.html">initialization functions</a>. If NULL, the default generator is used.<br /></td></tr>
</table>
Example :<br />
<p class="code"><pre>
C++ : // default generator
      TCODRandom * default = TCODRandom::getInstance();
      // save the state
      TCODRandom *backup=default->save();
      // get a random number (or several)
      int number1 = default->getInt(0,1000);
      // restore the state
      default->restore(backup);
      // get a random number
      int number2 = default->getInt(0,1000);
      // => number1 == number2
C   : /* save default generator state */
      TCOD_random_t backup=TCOD_random_save(NULL);
      /* get a random number */
      int number1 = TCOD_random_get_float(NULL,0,1000);
      /* restore the state */
      TCOD_random_restore(NULL,backup);
      /* get a random number */
      int number2 = TCOD_random_get_float(NULL,0,1000);
      /* number1 == number2 */
Py  : # save default generator state 
      backup=libtcod.random_save(0)
      # get a random number 
      number1 = libtcod.random_get_float(0,0,1000)
      # restore the state 
      libtcod.random_restore(0,backup)
      # get a random number 
      number2 = libtcod.random_get_float(0,0,1000)
      # number1 == number2 
</pre></p>
